package org.jeecg.modules.yfFdxx.service.impl;

import org.jeecg.modules.yfFdxx.entity.HqYfFdxx;
import org.jeecg.modules.yfFdxx.entity.HqYfFdxxYfyt;
import org.jeecg.modules.yfFdxx.entity.HqYfFdxxSssbywqk;
import org.jeecg.modules.yfFdxx.mapper.HqYfFdxxYfytMapper;
import org.jeecg.modules.yfFdxx.mapper.HqYfFdxxSssbywqkMapper;
import org.jeecg.modules.yfFdxx.mapper.HqYfFdxxMapper;
import org.jeecg.modules.yfFdxx.service.IHqYfFdxxService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 营房分栋信息
 * @Author: jeecg-boot
 * @Date:   2020-07-12
 * @Version: V1.0
 */
@Service
public class HqYfFdxxServiceImpl extends ServiceImpl<HqYfFdxxMapper, HqYfFdxx> implements IHqYfFdxxService {

	@Autowired
	private HqYfFdxxMapper hqYfFdxxMapper;
	@Autowired
	private HqYfFdxxYfytMapper hqYfFdxxYfytMapper;
	@Autowired
	private HqYfFdxxSssbywqkMapper hqYfFdxxSssbywqkMapper;


	@Override
	@Transactional
	public void saveMain(HqYfFdxx hqYfFdxx, List<HqYfFdxxYfyt> hqYfFdxxYfytList,List<HqYfFdxxSssbywqk> hqYfFdxxSssbywqkList) {
		hqYfFdxxMapper.insert(hqYfFdxx);
		if(hqYfFdxxYfytList!=null && hqYfFdxxYfytList.size()>0) {
			for(HqYfFdxxYfyt entity:hqYfFdxxYfytList) {
				//外键设置
				entity.setZhubId(hqYfFdxx.getId());
				hqYfFdxxYfytMapper.insert(entity);
			}
		}
		if(hqYfFdxxSssbywqkList!=null && hqYfFdxxSssbywqkList.size()>0) {
			for(HqYfFdxxSssbywqk entity:hqYfFdxxSssbywqkList) {
				//外键设置
				entity.setZhubId(hqYfFdxx.getId());
				hqYfFdxxSssbywqkMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(HqYfFdxx hqYfFdxx,List<HqYfFdxxYfyt> hqYfFdxxYfytList,List<HqYfFdxxSssbywqk> hqYfFdxxSssbywqkList) {
		hqYfFdxxMapper.updateById(hqYfFdxx);

		//1.先删除子表数据
		hqYfFdxxYfytMapper.deleteByMainId(hqYfFdxx.getId());
		hqYfFdxxSssbywqkMapper.deleteByMainId(hqYfFdxx.getId());

		//2.子表数据重新插入
		if(hqYfFdxxYfytList!=null && hqYfFdxxYfytList.size()>0) {
			for(HqYfFdxxYfyt entity:hqYfFdxxYfytList) {
				//外键设置
				entity.setZhubId(hqYfFdxx.getId());
				hqYfFdxxYfytMapper.insert(entity);
			}
		}
		if(hqYfFdxxSssbywqkList!=null && hqYfFdxxSssbywqkList.size()>0) {
			for(HqYfFdxxSssbywqk entity:hqYfFdxxSssbywqkList) {
				//外键设置
				entity.setZhubId(hqYfFdxx.getId());
				hqYfFdxxSssbywqkMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		hqYfFdxxYfytMapper.deleteByMainId(id);
		hqYfFdxxSssbywqkMapper.deleteByMainId(id);
		hqYfFdxxMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			hqYfFdxxYfytMapper.deleteByMainId(id.toString());
			hqYfFdxxSssbywqkMapper.deleteByMainId(id.toString());
			hqYfFdxxMapper.deleteById(id);
		}
	}

}
